package com.glowdayz.glowmee.bc;

import java.io.File;
import java.io.IOException;
import java.sql.PreparedStatement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.DateTime;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.glowmee.cmd.AdminTableCMD;
import com.glowdayz.glowmee.dao.AdminTableDAO;

public class UpdateNoticeBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public UpdateNoticeBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		int idAdminBoard = Integer.parseInt(request.getParameter("id"));
		
		try {
			AdminTableCMD cmd = AdminTableDAO.getInstance().getNotice(idAdminBoard);
			
			if (cmd.getImage() != null) {
				request.setAttribute("image", cmd.getImage());
			}
			request.setAttribute("cmd", cmd);
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/updateNotice.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		MultipartBox mbox = MultipartBox.getInstance();
		FileInfo finfo = mbox.upload2(request);
		
		int idAdminBoard = mbox.getInt("idAdminBoard");
		String boardTitle = mbox.getValue("boardTitle");
		String boardText = mbox.getValue("boardText");
		
		// 삭제할 파일에 대한 체크상태값
		String[] delete_file = mbox.getValues("imgDelete");
		
		try {
			AdminTableCMD cmd = AdminTableDAO.getInstance().getNotice(idAdminBoard);
			FileInfo image = cmd.getImage();
			if (image != null && finfo != null) {
				DBHandler.getInstance().beginTrans();
				if (finfo.getFieldName().equals("editImg")) {
					String edit = "UPDATE attachefile SET fileOrgName=?, fileSaveName=?, fileDir=?, fileSize=?, fileType=?, modified_date=? "
							+ "WHERE idAttacheFile=?";
					PreparedStatement pstmt = DBHandler.getInstance().getPrepareStatement(edit);
					pstmt.setString(1, finfo.getFileOrgName());
					pstmt.setString(2, finfo.getFileSaveName());
					pstmt.setString(3, finfo.getFileDir());
					pstmt.setLong(4, finfo.getFileSize());
					pstmt.setString(5, finfo.getFileType());
					pstmt.setString(6, DateTime.getInstance().getNowDateTimeString());
					pstmt.setInt(7, image.getIdAttacheFile());
					pstmt.executeUpdate();
					pstmt.close();
					pstmt = null;
					finfo = null;
					File f = new File(image.getFileDir() + "/" + image.getFileSaveName());
					if (f.exists()) {
						f.delete();
					}
				}
				DBHandler.getInstance().commit();
			}
		} catch (Exception e) {
			DBHandler.getInstance().rollback();
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		try {
			AdminTableDAO.getInstance().updateNotice(new AdminTableCMD(idAdminBoard, boardTitle, boardText, finfo), delete_file);
			
			AdminTableCMD cmd = AdminTableDAO.getInstance().getNotice(idAdminBoard);
			
			if (cmd.getImage() != null) {
				request.setAttribute("image", cmd.getImage());
			}
			
			request.setAttribute("cmd", cmd);
			request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.')</script>");
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/updateNotice.jsp");
		dpr.forward(request, response);
	}
}